package com.itheima;

import java.util.Random;

/**
 * 2025/3/31 20:20
 * 二维数组的打乱
 */
public class ArrayDemo2 {
    public static void main(String[] args) {
        int[][] numbers = {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
                {13, 14, 15, 16}
        };

        shuffle(numbers);

        for (int i = 0; i < numbers.length; i++) {
            for (int j = 0; j < numbers[i].length; j++) {
                System.out.print(numbers[i][j] + "\t");
            }
            System.out.println();
        }


    }

    public static void shuffle(int[][] numbers) {
        Random random = new Random();
        //打乱二维数组
        for (int i = 0; i < numbers.length; i++) {
            for (int j = 0; j < numbers[i].length; j++) {
                //随机生成一个行号
                int random1 = random.nextInt(numbers.length);
                //随机生成一个列号
                int random2 = random.nextInt(numbers[random1].length);
                //交换两个元素
                int temp = numbers[i][j];
                numbers[i][j] = numbers[random1][random2];
                numbers[random1][random2] = temp;
            }
        }
    }

}
